<template>
  <el-dialog :visible="showPermissionDialog" title="分配权限" @close="closeWinodow">
    分配权限
    <el-tree ref="permTree" node-key="id" :data="permissionList" :default-checked-keys="permIds" :props="{label:'name'} " :show-checkbox="true" :check-strictly="true" />
    <el-row type="flex" justify="center">
      <el-button @click="closeWinodow">取消</el-button>
      <el-button type="primary" @click="assignPerm">确认</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { getRoleDetail, assignPerm } from '@/api/setting'
import { departmentsTreeData } from '@/utils'
export default {
  props: {
    showPermissionDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionList: [],
      //   角色的权限id集合
      permIds: []
    }
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList()
      this.permissionList = departmentsTreeData(data, '0')
      console.log(this.permissionList)
    },
    async getRoleDetail() {
      const { permIds } = await getRoleDetail(this.id)
      this.permIds = permIds
      console.log(this.permIds)
    },
    async assignPerm() {
      const permIds = this.$refs.permTree.getCheckedKeys()
      console.log(permIds)
      await assignPerm({ id: this.id, permIds })
      this.$message.success('分配成功!')
      this.$emit('update:showPermissionDialog', false)
    },
    closeWinodow() {
      this.permIds = []
      this.$emit('update:showPermissionDialog', false)
    }
  }
}
</script>

<style>

</style>
